package com.sduept.nwld.dataserver.controller.document;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.utils.CharacterUtil;

@Named
@ViewScoped
public class DocumentController4DownLoad extends AbstractController {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String docPath;
	private StreamedContent file;

	
     
 	public void handleFileUpload(FileUploadEvent event) {
 		if(docPath!=null&&!docPath.isEmpty()) {
 			UploadedFile file = event.getFile();
 	 		try {
 	 			FileOutputStream fos = new FileOutputStream(docPath+file.getFileName());
 	 			fos.write(file.getContents());
 	 			fos.close();
 	 			addSuccessMessage("上传成功");
 	 		} catch (IOException ee) {
 	 			ee.printStackTrace();
 	 			addErrorMessage("上传失败");
 	 		}
 		}else {
 			addErrorMessage("请输入文件全路径");
 		}
 		
 	}
 // 文件下载
 	public void fileDownloadView() {
 		if(docPath==null||docPath.isEmpty()) {
 			addErrorMessage("文件路径不能为空！");
 		}else {
 			try {
 				File doc=new File(docPath);
 	 			InputStream stream = new FileInputStream(doc);
 	 			file=new DefaultStreamedContent(stream,"application/octet-stream",doc.getName() );
 	 		} catch (FileNotFoundException e) {
 	 			e.printStackTrace();
 	 			addErrorMessage("下载失败");
 	 		}
 		}
 	}

	public String getDocPath() {
		return docPath;
	}



	public void setDocPath(String docPath) {
		this.docPath = docPath;
	}
	public StreamedContent getFile() {
		return file;
	}
	public void setFile(StreamedContent file) {
		this.file = file;
	}

	
}
